package com.frame.metadata.wordbook;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.frame.database.JBaseDao;
import com.frame.database.JdataBean;
import com.frame.util.SelectItem;



public class WordbookDao extends JBaseDao{

	public WordbookDao(){
		super("wfwordbook_item");
	}

	public List<JdataBean> loadCatalogs(String parid) throws Exception{
		String sql="select id,name text,parid from Wfwordbook_catalog where ";
		Object[] objs=null;
		if(StringUtils.isNotBlank(parid)){sql+=" parid=?";objs=new Object[]{parid};}
		else sql+=" parid is null";
		sql+=" order by orderno,id";
		List<JdataBean> list=getSession().query(sql, objs);
		for(JdataBean d:list)
			loadSubCatalogs(d);
		return list;
	}
	private void loadSubCatalogs(JdataBean parnode) throws Exception{
		List<JdataBean> list=getSession().query("select id,name text,parid from Wfwordbook_catalog where parid =? order by orderno ,id", new Object[]{parnode.getString("id")});
		for(JdataBean d:list)
			loadSubCatalogs(d);
		parnode.put("children", list);
	}
	public List<JdataBean> loadItems(String code) throws Exception{
		String sql="select id,Item_name text from Wfwordbook_item where code=? and parid is null";
		sql+=" order by orderno,id";
		List<JdataBean> list=getSession().query(sql, new Object[]{code});
		for(JdataBean d:list)
			loadSubItems(d,true);
		return list;
	}
	private void loadSubItems(JdataBean parnode,boolean open) throws Exception{
		List<JdataBean> list=getSession().query("select id,Item_name text from Wfwordbook_item where parid=? order by orderno,id", new Object[]{parnode.getString("id")});
		for(JdataBean d:list)
			loadSubItems(d,false);
		if(list.size()>0){
			parnode.put("children", list);
			if(!open)parnode.put("state", "closed");
		}
	}
	public List<JdataBean> loadItemVals(String code) throws Exception{
		String sql="select Item_val id,Item_name text,id iid  from Wfwordbook_item where code=? and parid is null";
		sql+=" order by orderno,id";
		List<JdataBean> list=getSession().query(sql, new Object[]{code});
		for(JdataBean d:list)
			loadSubItemVals(d);
		return list;
	}
	private void loadSubItemVals(JdataBean parnode) throws Exception{
		List<JdataBean> list=getSession().query("select Item_val id,Item_name text,id iid from Wfwordbook_item where parid=? order by orderno ,id", new Object[]{parnode.getString("iid")});
		for(JdataBean d:list)
			loadSubCatalogs(d);
		parnode.put("children", list);
	}

	public JdataBean queryItemToBean(String code) throws Exception{
		JdataBean bean=new JdataBean();
		List<JdataBean> list=getSession().query("select Item_val value,Item_name text from Wfwordbook_item where lower(code)=?",new Object[]{code.toLowerCase()});
		for(JdataBean d:list){
			bean.put(d.getString("value"), d.getString("text"));
		}
		return bean;
	}
	public List<SelectItem> queryItem(String code) throws Exception{
		return getSession().query("select Item_val value,Item_name text from Wfwordbook_item where lower(code)=? and parid is null order by orderno,id",new Object[]{code.toLowerCase()},SelectItem.class);
	}
	public String getItemName(String val,JdataBean itemBean){
		if(itemBean==null)return val;
		String name=itemBean.getString(val);
		if(StringUtils.isBlank(name))return val;
		else return name;
	}
	public String getItemName(String val,String code){
		String name="";
		try{
			JdataBean d=getSession().getObject("select * from Wfwordbook_item where lower(code)=? and Item_val=?", new Object[]{code.toLowerCase(),val});
			if(d!=null)name=d.getString("Item_name");
		}catch(Exception e){e.printStackTrace();}
		if(StringUtils.isBlank(name))return val;
		return name;
	}
}
